
(module count-occurrences (lib "eopl.ss" "eopl")
  (provide count-occurrences)
;;1.18
;; count-occurrences: Symbol * List -> Number
;; usage: (count-occurrences s slist)
;; produces: return the number of occurces of s in slist

;; examples:
;; (count-occurrences 'x '((f x))) ==> 1
;; (count-occurrences 'x '((f x) y (((x z) () x)))) ==> 3
;; (count-occurrences 'w '((f x) y (((x z) x)))) ==> 0

 

(define (count-occurrences s slist)
  (cond
    ( (null? slist) 0)
    ( (pair? (car slist) ) (+ (count-occurrences s (car slist)) (count-occurrences s (cdr slist))))
    ( (eq? (car slist) s) (+ 1 (count-occurrences s (cdr slist))))
    ( else  (count-occurrences s (cdr slist)))
   )
)

 

(count-occurrences 'x '((f x)))
(count-occurrences 'x '((f x) y (((x z) () x))))
(count-occurrences 'w '((f x) y (((x z) x))))

)